<?php

namespace app\admin\controller\userwork;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Daywork extends Base
{
    protected $beforeActionList = [
        'first'
    ];

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        if ($this->project_user == 0) {
            $list = DB::view('auth_group_access', 'uid')
                ->view('admin', 'nickname', 'auth_group_access.uid=admin.id')
                ->where('status', 1)
                ->distinct(true)
                ->select();
            $this->assign('user_list', $list);

        }
        return view();
    }
    protected $project_user = 0;

    protected function first()
    {
        $group = DB::name('auth_group_access')->where('uid', session('uid'))->select();
        $group_ids = array_column($group, 'group_id');
       // dump($group_ids);
        //判断当前用户身份
        if (in_array(13, $group_ids) || in_array(1, $group_ids)) {
            //管理员
            $this->project_user = 0;
        } else {
            //开发人员
            $this->project_user = session('uid');
        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['title', 'like', '%' . $post['title'] . '%'];
        }
        if ($this->project_user == 0) {
            if (empty($post['uid']) || $post['uid'] == '') {
            } else {
                $map[] = ['work_time.uid', '=', $post['uid']];
            }
        } else {
            $map[] = ['work_time.uid', '=', $this->project_user];
        }

        $data['count'] = Db::view('work_time', 'id,c_time,uid,w_time,project_id,stories_id')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->view('project', 'title', 'work_time.project_id=project.id')
            ->view('stories',['title'=>'stories_title'],'work_time.stories_id=stories.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('work_time', 'id,c_time,uid,w_time,project_id')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->view('project', 'title', 'work_time.project_id=project.id')
            ->view('stories',['title'=>'stories_title'],'work_time.stories_id=stories.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('work_time.id desc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['c_time'] = date('Y-m-d  H:i:s', $info['c_time']);
            $info['title']='['.$info['title'].']'.$info['stories_title'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function editform()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            $info = DB::name('user_work')->where(['id' => $post['id']])->find();
            if (!$info) {
                $this->error('参数有误');
            }
            $data['sum_wage'] = $info['work_wage'] + $info['base_wage'] + $post['change_wage'];
            $data['change_wage'] = $post['change_wage'];
            Db::name('user_work')->where(['id' => $post['id']])->update($data);
            Db::startTrans();
            try {

                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }

    }


}